<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入名称"
            :maxLength="20"
            v-decorator="['name', { rules: [{ required: true, message: '请输入名称！' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
    <div class="drawer-footer">
      <a-button class="left" @click="handleCancel"> 关闭 </a-button>
      <a-button class="right" type="primary" @click="handleSubmit"> 提交 </a-button>
    </div>
  </a-drawer>
</template>

<script>
import { homeworkTagAdd, homeworkTagEdit } from '@/api/modular/system/homeworkTagManage'
export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      id: undefined,
      title: '',
    }
  },
  methods: {
    // 初始化方法
    open(record) {
      if (record) {
        this.id = record.id
        this.title = '编辑作业标签'
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id,
            name: record.name,
          })
        }, 100)
      } else {
        this.id = ''
        this.title = '新增作业标签'
      }
      this.visible = true
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof values[key] === 'object' && values[key] != null) {
              values[key] = JSON.stringify(values[key])
            }
          }

          if (this.id) {
            homeworkTagEdit(values)
              .then((res) => {
                if (res.success) {
                  this.$message.success('编辑成功')
                  this.confirmLoading = false
                  this.$emit('ok', values)
                  this.handleCancel()
                } else {
                  this.$message.error('编辑失败') //  + res.message
                }
              })
              .finally((res) => {
                this.confirmLoading = false
              })
          } else {
            homeworkTagAdd(values)
              .then((res) => {
                if (res.success) {
                  this.$message.success('新增成功')
                  this.confirmLoading = false
                  this.$emit('ok', values)
                  this.handleCancel()
                } else {
                  this.$message.error('新增失败') // + res.message
                }
              })
              .finally((res) => {
                this.confirmLoading = false
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>
